  var Order_tr = false;
  var Order_td = false;
  var Order_type_sel = false;
  var TPlayer_sel = false;
  var TPlayer_span = false;
  var Destination_sel = false;
  var Destination_span = false;
  var TGroup_sel = false;
  var TGroup_span = false;
  var Order_type_jsid = CW2_NO_OBJECT;
  var Order_tplayer_jsid = CW2_NO_OBJECT;
  var Order_destination_jsid = CW2_NO_OBJECT;
  var Order_tgroup_jsid = CW2_NO_OBJECT;
  
  var CW2_ORDER_TO_SEND = 1;
  var CW2_ORDER_SENDING = 2;
  var CW2_ORDER_UNCONFIRMED = 0;
  var CW2_ORDER_IN_SQL = 5;
  var CW2_ORDER_CONFIRMED = 6;
  
  var CW2_Send_Status = 1;

  var Opened_order_hid = "";
  var Opened_order_jsid = CW2_NO_OBJECT;

function C_Tournament(neutral_tgroup_jsid)
{
 this.glicko_min = 0;
 this.glicko_max = 0;
 this.neutral_tgroup_jsid = neutral_tgroup_jsid;
}
  
function C_Order_Type(order_sid, order_text, order_permissions, tplayer_permissions, align, has_destination, non_neutral, has_tgroup)
{
 this.order_sid = order_sid;
 this.order_text = order_text;
 this.order_permissions = order_permissions;
 this.tplayer_permissions = tplayer_permissions;
 this.align = align;
 this.has_destination = has_destination;
 this.non_neutral = non_neutral;
 this.has_tgroup = has_tgroup;
}
var Order_types = new Array();

function C_Order(original_order_jsid, order_jsid, order_sid, order_type_jsid, tournament_sid, turn, destination_jsid, tplayer_jsid, tgroup_jsid, unit_jsid, comment)
{
 this.original_order_jsid = original_order_jsid;
 this.edit_order_jsid = CW2_NO_OBJECT;
 this.order_jsid = order_jsid;

 this.getAlign_jsid = function()
 {
  if ((this.order_align == CW2_ORDER_PLANET) || (this.order_align == CW2_ORDER_FORT))
  {
   return this.destination_jsid;
  }
  else if (this.order_align & CW2_ORDER_UNIT)
  {
   return this.unit_jsid;
  }
  else
  {
   return CW2_NO_SID;
  }
 }

 //******
 
 if (original_order_jsid >= 0) //copying it from an original order
 {
  this.order_sid = Orders[original_order_jsid].order_sid;
  this.order_type_jsid = Orders[original_order_jsid].order_type_jsid;
  this.tournament_sid = Orders[original_order_jsid].tournament_sid;
  this.turn = Orders[original_order_jsid].turn;
  this.destination_jsid = Orders[original_order_jsid].destination_jsid;
  this.tplayer_jsid = Orders[original_order_jsid].tplayer_jsid;
  this.tgroup_jsid = Orders[original_order_jsid].tgroup_jsid;
  this.unit_jsid = Orders[original_order_jsid].unit_jsid;
  this.comment = Orders[original_order_jsid].comment;
  this.order_align = Orders[original_order_jsid].order_align;
  
  this.js_status = CW2_ORDER_UNCONFIRMED;
  
  this.align_hid = Orders[original_order_jsid].order_hid;
  
  return;
 }
 
 //******
 
 this.order_sid = order_sid;
 this.order_type_jsid = order_type_jsid;
 this.tournament_sid = tournament_sid;
 this.turn = turn;
 this.destination_jsid = destination_jsid;
 this.tplayer_jsid = tplayer_jsid;
 this.tgroup_jsid = tgroup_jsid;
 this.unit_jsid = unit_jsid;
 this.comment = comment;
 
 this.js_status = CW2_ORDER_CONFIRMED;
}
var Orders = new Array();

function C_Planet(planet_sid, planet_name, planet_flags, tgroup_jsid, tgroup_color, starlanes_jsids)
{
 this.planet_sid = planet_sid;
 this.planet_name = planet_name;
 this.tgroup_jsid = tgroup_jsid;
 this.tgroup_color = tgroup_color;
 this.starlanes_jsids = starlanes_jsids;
 this.planet_flags = planet_flags;
 this.new_order_jsid = CW2_NO_OBJECT;
 this.num_new_orders = 0;
}
var Planets = new Array();

function C_TPlayer(tplayer_sid, tplayer_name, tgroup_jsid, cw2_player_flags, glicko)
{
 this.tplayer_sid = tplayer_sid;
 this.name = tplayer_name;
 this.tgroup_jsid = tgroup_jsid;
 this.cw2_player_flags = cw2_player_flags;
 this.permissions = cw2_player_flags & CW2_MAX_HUMAN_PERMISSIONS;
 this.glicko = glicko;
 this.num_armies = 0;
 this.num_garrisons = 0;
 this.recruiting_armies = 0;
 this.recruiting_garrisons = 0;
}
var TPlayers = new Array();

function C_TGroup(tgroup_sid, tgroup_name, tgroup_color, credits)
{
 this.tgroup_sid = tgroup_sid;
 this.tgroup_name = tgroup_name;
 this.tgroup_color = tgroup_color;
 this.credits = credits;
 this.spent_credits = 0;
 this.building_forts = 0;
}
var TGroups = new Array();

function C_Unit(unit_sid, tplayer_jsid, tgroup_jsid, planet_jsid, unit_flags)
{
 this.unit_sid = unit_sid;
 this.tplayer_jsid = tplayer_jsid;
 this.tgroup_jsid = tgroup_jsid;
 this.planet_jsid = planet_jsid;
 this.unit_flags = unit_flags;
 this.order_jsid = CW2_NO_OBJECT;
}
var Units = new Array();

function C_Permission(permission_sid, permission_name)
{
 this.permission_sid = permission_sid;
 this.permission_name = permission_name;
}
var Permission_types = new Array();

function dAl(sendit, txt, log_it)
{
 if (!sendit) {return;}
 if (CW2_DEBUG)
 {
  if (log_it)
  {
   document.getElementById("cw2_log").innerHTML += txt + "<br>";
  }
  else
  {
   alert(txt);
  }
 }
}

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function insertAfter(new_node, existing_node)
{
 if (existing_node.nextSibling)
 {
  before_node = existing_node.nextSibling;
  while ((before_node.for_edit) && (before_node.nextSibling))
  {
   before_node = before_node.nextSibling;
  }
  if (before_node.for_edit)
  {
   before_node.parentNode.appendChild(new_node);
  }
  else
  {
   before_node.parentNode.insertBefore(new_node, before_node);
  }
 }
 else
 {
  existing_node.parentNode.appendChild(new_node);
 }
}

function permissionsToTxt(permission_sid)
{
 txt = "";
 for (perm_i in Permission_types)
 {
  if (Permission_types[perm_i].permission_sid & permission_sid)
  {
   if (txt != "")
   {
    txt += " | ";
   }
   txt += Permission_types[perm_i].permission_name;
  }
 }
 return txt;
}

function hideOldOrder()
{
 if (Opened_order_hid != "")
 {
  el = document.getElementById(Opened_order_hid);
  el.style.display = "none";
  Opened_order_hid = "";
 }
}

function addOption(sel, value, text)
{
 sel.options[sel.options.length] = new Option("'"+value+"' "+text, value);
}

function selectSetDefault(sel, def_value)
{
 idx = -1;
 for (idx_i in sel.options)
 {
  if (sel.options[idx_i].value == def_value)
  {
   idx = idx_i;
  }
 }
 if (idx >= 0)
 {
  sel.selectedIndex = idx;
 }
}

function fillCurrOrderData(order_jsid, set_default_values)
{
 Order_tr = document.getElementById(Orders[order_jsid].order_hid);
 dAl(1, "order_hid="+Orders[order_jsid].order_hid+"  Order_tr.id = "+Order_tr.id+"  set_default_values="+set_default_values, 1);
 Order_td = Order_tr.childNodes[0];
 len = Order_td.childNodes.length;
 Order_type_sel = false;
 TPlayer_sel = false;
 TPlayer_span = false;
 Destination_span = false;
 Destination_sel = false;
 TGroup_span = false;
 TGroup_sel = false;
 Order_type_jsid = CW2_NO_OBJECT;
 Order_tplayer_jsid = CW2_NO_OBJECT;
 Order_tgroup_jsid = CW2_NO_OBJECT;
 
 for (otc_i in Order_td.childNodes)
 {
  el = Order_td.childNodes[otc_i];
  if (el.ots)
  {
   Order_type_sel = el;
   if (set_default_values)
   {
    Order_type_jsid = Orders[order_jsid].order_type_jsid;
    selectSetDefault(Order_type_sel, Order_type_jsid);
   }
   idx = Order_type_sel.selectedIndex;
   if (idx >= 0)
   {
    Order_type_jsid = Order_type_sel.options[idx].value;
    Orders[order_jsid].order_type_jsid = Order_type_jsid;
   }
  }
  else if (el.otpl)
  {
   TPlayer_span = el;
   TPlayer_sel = TPlayer_span.childNodes[0];
   if (set_default_values)
   {
    Order_tplayer_jsid = Orders[order_jsid].tplayer_jsid;
    selectSetDefault(TPlayer_sel, Order_tplayer_jsid);
   }
   idx = TPlayer_sel.selectedIndex;
   if (idx >= 0)
   {
    Order_tplayer_jsid = TPlayer_sel.options[idx].value;
    Orders[order_jsid].tplayer_jsid = Order_tplayer_jsid;
   }
  }
  else if (el.otdest)
  {
   Destination_span = el;
   Destination_sel = Destination_span.childNodes[0];
   if (set_default_values)
   {
    Order_destination_jsid = Orders[order_jsid].destination_jsid;
    selectSetDefault(Destination_sel, Order_destination_jsid);
   }
   idx = Destination_sel.selectedIndex;
   if (idx >= 0)
   {
    Order_destination_jsid = Destination_sel.options[idx].value;
    Orders[order_jsid].destination_jsid = Order_destination_jsid;
   }
  }
  else if (el.ottgroup)
  {
   TGroup_span = el;
   TGroup_sel = TGroup_span.childNodes[0];
   if (set_default_values)
   {
    Order_tgroup_jsid = Orders[order_jsid].tgroup_jsid;
    selectSetDefault(TGroup_sel, Order_tgroup_jsid);
   }
   idx = TGroup_sel.selectedIndex;
   if (idx >= 0)
   {
    Order_tgroup_jsid = TGroup_sel.options[idx].value;
    Orders[order_jsid].tgroup_jsid = Order_tgroup_jsid;
   }
  }
 }
}

function onOrderTypeChange(order_jsid, object_jsid, order_align, tgroup_jsid, set_default_values)
{
 if (order_jsid < 0)
 {
  set_default_values = false;
 } 
 fillCurrOrderData(order_jsid, set_default_values);
 if ((Order_type_jsid >= 0) && Order_types[Order_type_jsid].tplayer_permissions)
 {
  if (TPlayer_sel.tplayer_permissions != Order_types[Order_type_jsid].tplayer_permissions)
  {
   TPlayer_sel.options.length = 0;
   for (TPlayer_jsid in TPlayers)
   {
    if (TPlayers[TPlayer_jsid].permissions < Order_types[Order_type_jsid].tplayer_permissions) {continue;};
    if (TPlayers[TPlayer_jsid].tgroup_jsid != tgroup_jsid) {continue;}
    addOption(TPlayer_sel, TPlayer_jsid, TPlayers[TPlayer_jsid].name);
   }
   TPlayer_sel.tplayer_permissions = Order_types[Order_type_jsid].tplayer_permissions;
  }
  TPlayer_sel.style.display = "";
 }
 else
 {
  TPlayer_sel.style.display = "none";
 }

 if ((Order_type_jsid >= 0) && Order_types[Order_type_jsid].has_tgroup)
 {
  TGroup_sel.options.length = 0;
  dAl(1, "has_tgroup", 1);
  if (order_align == CW2_ORDER_PLANET)
  {
   curr_tgroup_jsid = Planets[object_jsid].planet_jsid;
   for (tgroup_i in TGroups)
   {
    if ((tgroup_i != curr_tgroup_jsid) && (tgroup_i != Tournament.neutral_tgroup_jsid))
    {
     addOption(TGroup_sel, tgroup_i, TGroups[tgroup_i].tgroup_name);
    }
   }
  }
 }
 
 if ((Order_type_jsid >= 0) && Order_types[Order_type_jsid].has_destination)
 {
  Destination_sel.options.length = 0;
  dAl(0, "has_dest", 1);
  if (Order_types[Order_type_jsid].order_permissions & CW2_CLAN_OWN_UNIT_ORDERS)
  {
   dAl(0, "CW2_CLAN_OWN_UNIT_ORDERS", 1);
   if (order_align == CW2_ORDER_UNIT)
   {
    source_jsid = Units[object_jsid].planet_jsid;
    dAl(0, "order_align == CW2_ORDER_UNIT  source planet jsid="+source_jsid, 1);
    for (dest_i in Planets[source_jsid].starlanes_jsids)
    {
     dest_jsid = Planets[source_jsid].starlanes_jsids[dest_i];
     dAl(0, "dest_jsid="+dest_jsid+" garrison="+(Units[object_jsid].unit_flags & CW2_UNIT_GARRISON), 1);
     if (dest_jsid < 0) {continue;}
     if (Planets[dest_jsid].planet_flags & CW2_PLANET_ONGOING_COMBAT) {continue;}
     dAl(0, "planet tgroup= "+Planets[dest_jsid].tgroup_jsid+" unit tgroup="+Units[object_jsid].tgroup_jsid, 1);
     if (Planets[dest_jsid].tgroup_jsid != Units[object_jsid].tgroup_jsid)
     {
      if (Units[object_jsid].unit_flags & CW2_UNIT_GARRISON) {continue};
      if ((Current_turn == 1) && (Planets[dest_jsid].planet_flags & CW2_PLANET_FORT)) {continue;} 
     }
     addOption(Destination_sel, dest_jsid, Planets[dest_jsid].planet_name);
    }
   }
  }
  else if (Order_types[Order_type_jsid].order_permissions & CW2_TOURNAMENT_MODERATOR)
  {
   for (dest_jsid in Planets)
   {
    addOption(Destination_sel, dest_jsid, Planets[dest_jsid].planet_name);
   }
  }
  Destination_sel.style.display = "";
 } 
 else
 {
  Destination_sel.style.display = "none";
 }
}

function isOrderOptionAllowed(order_jsid, order_type_jsid, order_align, tgroup_jsid)
{
 order_type = Order_types[i_order_type_jsid];
 
 if (!(order_type.align & order_align))
 {
  dAl(0, ""+order_type.order_text+":  align "+Order_types[i_order_type_jsid].align+"~"+order_align, 1);
  return false;
 }
 
 if ((order_type.align == CW2_ORDER_PLANET) && (order_type.non_neutral))
 {
  planet_jsid = Orders[order_jsid].destination_jsid;
  tgroup_jsid = Planets[planet_jsid].tgroup_jsid;
  if (tgroup_jsid == Tournament.neutral_tgroup_jsid)
  {
   dAl(0, ""+order_type.order_text+":  neutral planet "+tgroup_jsid+"="+Tournament.neutral_tgroup_jsid, 1);
   return false;
  }
 }
 
 if (!(Logged_permissions & order_type.order_permissions))
 {
  dAl(0, ""+order_type.order_text+":  logged_permissions "+permissionsToTxt(Logged_permissions)+", need "+permissionsToTxt(Order_types[i_order_type_jsid].order_permissions), 1);
  return false;
 }
 if ((Logged_permissions <= CW2_MAX_CLAN_PERMISSIONS) && (tgroup_jsid != Logged_tgroup_jsid))
 {
  dAl(0, ""+order_type.order_text+":  logged tgroup jsid "+Logged_tgroup_jsid+", need "+tgroup_jsid, 1);
  return false;
 }
 
 if (order_type.tplayer_permissions)
 {
  if (!(Logged_permissions & order_type.tplayer_permissions))
  {
   dAl(0, ""+order_type.order_text+":  tplayer_permissions "+permissionsToTxt(Logged_permissions)+", need "+permissionsToTxt(Order_types[i_order_type_jsid].tplayer_permissions), 1);
   return false;
  }
  if ((order_type.tplayer_permissions == Logged_permissions) && (tgroup_jsid != Logged_tgroup_jsid))
  {
   dAl(0, ""+order_type.order_text+":  tplayer_permissions "+permissionsToTxt(Logged_permissions)+", need "+permissionsToTxt(Order_types[i_order_type_jsid].tplayer_permissions)+" and tgroup jsid "+Logged_tgroup_jsid+", need "+permissionsToTxt(tgroup_jsid), 1);
   return false;
  }
 }

 if ((order_type.order_sid & CW2_ORDER_MAIN) == CW2_ORDER_MOVE)
 {
  unit_jsid = Orders[order_jsid].unit_jsid;
  if (unit_jsid < 0)
  {
   dAl(0, "unit_jsid "+unit_jsid, 1);
   return false;
  }
  planet_jsid = Units[unit_jsid].planet_jsid;
  if (planet_jsid < 0)
  {
   dAl(0, "planet_jsid "+planet_jsid, 1);
   return false;
  }
  
  if (Planets[planet_jsid].planet_flags & CW2_PLANET_ONGOING_COMBAT)
  {
   dAl(0, ""+order_type.order_text+":  planet "+planet_jsid+" - ongoing combat ("+Planets[planet_jsid].planet_flags+")", 1);
   return false;
  }
 }
 
 if ((order_type.order_sid & CW2_ORDER_MAIN) == CW2_ORDER_RECRUIT)
 {
  if (order_type.order_sid & CW2_ORDER_UNIT)
  {
   dAl(0, ""+order_type.order_text+":  ok 3", 1);
   planet_jsid = Orders[order_jsid].destination_jsid;
   if (planet_jsid < 0)
   {
    dAl(0, ""+order_type.order_text+":  planet_jsid "+planet_jsid, 1);
    return false;
   }
   if (!(Planets[planet_jsid].planet_flags & CW2_PLANET_FORT))
   {
    dAl(0, ""+order_type.order_text+":  planet "+planet_jsid+" - no fort ("+Planets[planet_jsid].planet_flags+")", 1);
    return false;
   }
  }
  else if (order_type.order_sid & CW2_ORDER_FORT)
  {
   if (Planets[planet_jsid].planet_flags & CW2_PLANET_FORT)
   {
    dAl(0, ""+order_type.order_text+":  planet "+planet_jsid+" - already has fort ("+Planets[planet_jsid].planet_flags+")", 1);
    return false;
   }
   planet_jsid = Orders[order_jsid].destination_jsid;
   for (tmp_order_jsid in Orders)
   {
    if ((tmp_order_jsid != order_jsid) && (Orders[tmp_order_jsid].order_sid & CW2_ORDER_FORT) && (Orders[tmp_order_jsid].destination_jsid == planet_jsid))
    {
     if (((Orders[tmp_order_jsid].order_sid & CW2_ORDER_MAIN) == CW2_ORDER_CREATE) || ((Orders[tmp_order_jsid].order_sid & CW2_ORDER_MAIN) == CW2_ORDER_RECRUIT))
     {
      dAl(1, ""+order_type.order_text+":  planet "+planet_jsid+" - already has 'build fort' order ("+tmp_order_jsid+")", 1);
      return false;
     }
    }
   }
  }
 }

 dAl(0, ""+order_type.order_text+":  ok TRUE", 1); 
 return true;
}

function isOrderAllowed(order_jsid, order_align, tgroup_jsid)
{
 order_type_jsid = Orders[order_jsid].order_type_jsid;
 for (i_order_type_jsid in Order_types)
 {
  if (isOrderOptionAllowed(order_jsid, i_order_type_jsid, order_align, tgroup_jsid))
  {
   dAl(0, "order_jsid="+order_jsid+" i_order_type_jsid="+i_order_type_jsid+" order_type_jsid="+order_type_jsid+" Orders[order_jsid].order_type_jsid="+Orders[order_jsid].order_type_jsid, 1);
   if ((order_type_jsid < 0) || (order_type_jsid == i_order_type_jsid))
   {
    return true;
   }
  }
 }
 return false;
}

function findNewOrder_jsid()
{
  return (Orders.length);
  //new every time
  for (i in Orders)
  {
   if (Orders[i].order_sid == CW2_NO_OBJECT)
   {
    return i;
   }
  }
}

function closeOrder(order_jsid)
{
 if (order_jsid < 0) {return;}
 
 tr = document.getElementById(Orders[order_jsid].order_hid);
 if (tr)
 {
  tr.style.display = "none";
 }
 Opened_order_jsid = CW2_NO_OBJECT;
 
 if (Orders[order_jsid].js_status = CW2_ORDER_UNCONFIRMED)
 {
   
  
 }
}

function objectOrdersClick(order_jsid, object_jsid, order_align, tgroup_jsid)
{
 
 tr = document.getElementById(Orders[order_jsid].order_hid);
 if (Opened_order_jsid == Orders[order_jsid].order_hid) //we clicked same order, close order
 {
  closeOrder(order_jsid);
  tr.style.display = "none";
  Opened_order_hid = "";
  return;
 }
 else if (Opened_order_hid)
 {
  old_tr = document.getElementById(Opened_order_hid);
  old_tr.style.display = "none";
  Opened_order_hid = "";
 }

 dAl(1, "order_jsid="+order_jsid+" object_jsid="+object_jsid+" order_align="+order_align+" tgroup_jsid="+tgroup_jsid, 1);
 if (!isOrderAllowed(order_jsid, order_align, tgroup_jsid))
 {
  return;
 }
 
 if (order_align & CW2_ORDER_PLANET)
 {
  Planets[object_jsid].new_order_jsid = CW2_NO_OBJECT;
 }
 
 if (!tr)
 {
  object_tr = document.getElementById(Orders[order_jsid].align_hid);
  tr = document.createElement('tr');
  tr.id = Orders[order_jsid].order_hid;
  tr.for_edit = 1;
  td = document.createElement('td');
  td.colSpan = 2;
  tr.appendChild(td);

  sel_ordertype = document.createElement('select');
  for (i_order_type_jsid in Order_types)
  {
   if (isOrderOptionAllowed(order_jsid, order_type_jsid, order_align, tgroup_jsid))
   {
    addOption(sel_ordertype, i_order_type_jsid, Order_types[i_order_type_jsid].order_text);
   }
  }
  sel_ordertype.ots = 1;
  sel_ordertype.setAttribute('onclick', 'onOrderTypeChange('+order_jsid+', '+object_jsid+', '+order_align+', '+tgroup_jsid+', false)');
  td.appendChild(sel_ordertype);
  
  tplayer_span = document.createElement('span');
  tplayer_span.otpl = 1;
  tplayer_sel = document.createElement('select');
  tplayer_sel.tplayer_permissions = 0;
  tplayer_span.appendChild(tplayer_sel);
  td.appendChild(tplayer_span);
  
  destination_span = document.createElement('span');
  destination_span.otdest = 1;
  destination_sel = document.createElement('select');
  destination_span.appendChild(destination_sel);
  td.appendChild(destination_span);
  
  tgroup_span = document.createElement('span');
  tgroup_span.ottgroup = 1;
  tgroup_sel = document.createElement('select');
  tgroup_span.appendChild(tgroup_sel);
  td.appendChild(tgroup_span);
  
  td.appendChild(document.createElement('br'));
  
  btn_apply = document.createElement('input');
  btn_apply.type = "button";
  btn_apply.setAttribute('onclick', 'applyCancelOrder('+order_jsid+', "'+Orders[order_jsid].order_hid+'", 1, '+order_align+', '+object_jsid+')');
  btn_apply.value = Lang_cw2_order_apply;
  td.appendChild(btn_apply);
  
  btn_cancel = document.createElement('input');
  btn_cancel.type = "button";
  btn_cancel.setAttribute('onclick', 'applyCancelOrder('+order_jsid+', "'+Orders[order_jsid].order_hid+'", -1, '+order_align+', '+object_jsid+')');
  btn_cancel.value = Lang_cw2_order_cancel;
  td.appendChild(btn_cancel);
  
  insertAfter(tr, object_tr);
 }
 else
 {
  fillCurrOrderData(order_jsid, false);
  Order_type_sel.options.length = 0;
  for (i_order_type_jsid in Order_types)
  {
   if (isOrderOptionAllowed(order_jsid, order_type_jsid, order_align, tgroup_jsid))
   {
    addOption(Order_type_sel, i_order_type_jsid, Order_types[i_order_type_jsid].order_text);
   }
  }
 }
 
 hideOldOrder();
 onOrderTypeChange(order_jsid, object_jsid, order_align, tgroup_jsid, true);
 tr.style.display = "";
 Opened_order_hid = Orders[order_jsid].order_hid;
}

function orderClick(jsid)
{
 edit_order_jsid = jsid;
 if (Orders[jsid].edit_order_jsid >= 0)
 {
  edit_order_jsid = Orders[jsid].edit_order_jsid;
 }
 else if ((Orders[jsid].original_order_jsid < 0) && (Orders[jsid].order_sid <= 0)) //accepted order and no temp order to change it
 {
  edit_order_jsid = findNewOrder_jsid();
  Orders[edit_order_jsid] = new C_Order(jsid, edit_order_jsid);
  Orders[edit_order_jsid].order_hid = "order_" + edit_order_jsid;
  Orders[jsid].edit_order_jsid = edit_order_jsid;
 }
 objectOrdersClick(edit_order_jsid, Orders[edit_order_jsid].getAlign_jsid(), Orders[edit_order_jsid].order_align, Orders[edit_order_jsid].tgroup_jsid);
}

function planetOrdersClick(planet_jsid)
{
 tgroup_jsid = Planets[planet_jsid].tgroup_jsid;
 new_order_jsid = Planets[planet_jsid].new_order_jsid;
 if (new_order_jsid < 0)
 {
  new_order_jsid = findNewOrder_jsid();
  Planets[planet_jsid].new_order_jsid = new_order_jsid;
 }
 
 Orders[new_order_jsid] = new C_Order(CW2_NO_OBJECT, new_order_jsid, CW2_NO_SID, CW2_NO_OBJECT, Tournament_id, Current_turn, planet_jsid, CW2_NO_OBJECT, tgroup_jsid, CW2_NO_OBJECT, "");
 Orders[new_order_jsid].order_align = CW2_ORDER_PLANET;
 Orders[new_order_jsid].order_hid = 'planet_new_order_'+planet_jsid+"_"+Planets[planet_jsid].num_new_orders;
 Orders[new_order_jsid].align_hid = 'planet_'+planet_jsid;
 Planets[planet_jsid].num_new_orders++;

 objectOrdersClick(new_order_jsid, planet_jsid, CW2_ORDER_PLANET, Planets[planet_jsid].tgroup_jsid);
}

function fortOrdersClick(planet_jsid)
{
 fort_order_jsid = Planets[planet_jsid].fort_order_jsid;
 if (fort_order_jsid >= 0)
 {
  orderClick(fort_order_jsid);
  return;
 }
 
 tgroup_jsid = Planets[planet_jsid].tgroup_jsid;
 new_order_jsid = findNewOrder_jsid();
 Planets[planet_jsid].fort_order_jsid = new_order_jsid;
 Orders[new_order_jsid] = new C_Order(CW2_NO_OBJECT, new_order_jsid, CW2_NO_SID, CW2_NO_OBJECT, Tournament_id, Current_turn, planet_jsid, CW2_NO_OBJECT, tgroup_jsid, CW2_NO_OBJECT, "");
 Orders[new_order_jsid].order_align = CW2_ORDER_FORT;
 Orders[new_order_jsid].order_hid = 'fort_new_order_'+planet_jsid;
 Orders[new_order_jsid].align_hid = 'fort_'+planet_jsid;
 
 objectOrdersClick(new_order_jsid, planet_jsid, CW2_ORDER_FORT, tgroup_jsid);
}

function unitOrdersClick(unit_jsid)
{
 if (Units[unit_jsid].order_jsid >= 0)
 {
  orderClick(Units[unit_jsid].order_jsid);
  return;
 }
 
 tgroup_jsid = Units[unit_jsid].tgroup_jsid;
 
 new_order_jsid = findNewOrder_jsid();
 Orders[new_order_jsid] = new C_Order(CW2_NO_OBJECT, new_order_jsid, CW2_NO_SID, CW2_NO_OBJECT, Tournament_id, Current_turn, CW2_NO_OBJECT, CW2_NO_OBJECT, tgroup_jsid, unit_jsid, "");
 Units[unit_jsid].order_jsid = new_order_jsid;
 Orders[new_order_jsid].order_align = CW2_ORDER_UNIT;
 Orders[new_order_jsid].order_hid = 'unit_new_order_'+unit_jsid;
 Orders[new_order_jsid].align_hid = 'unit_'+unit_jsid;
 
 objectOrdersClick(Units[unit_jsid].order_jsid, unit_jsid, CW2_ORDER_UNIT, tgroup_jsid);
}

function sendOrders()
{
 if (CW2_Send_Status != 1) {return;}
 
 params = "main_player_id="+Logged_player_id;
 params += "&main_tournament_id="+Tournament_id;
 order_list = "";
 
 num_orders_to_send = 0;
 for (jsid in Orders)
 {
  if (Orders[jsid].js_status == CW2_ORDER_TO_SEND)
  {
   num_orders_to_send++;
   params += "&order_jsid[]="+Orders[jsid].order_jsid;
   params += "&order_id[]="+Orders[jsid].order_sid;
   params += "&order_type[]="+Order_types[Orders[jsid].order_type_jsid].order_sid;
   params += "&tournament_id[]="+Orders[jsid].tournament_sid;
   params += "&turn[]="+Orders[jsid].turn;
   
   destination_jsid = Orders[jsid].destination_jsid;
   if (destination_jsid >= 0)
   {
    params += "&destination_id[]="+Planets[destination_jsid].planet_sid;
   }
   else
   {
    params += "&destination_id[]="+CW2_NO_OBJECT;
   }
   
   tplayer_jsid = Orders[jsid].tplayer_jsid;
   if (tplayer_jsid >= 0)
   {
    params += "&tplayer_id[]="+Players[tplayer_jsid].tplayer_sid;
   }
   else
   {
    params += "&tplayer_id[]="+CW2_NO_OBJECT;
   }
   
   tgroup_jsid = Orders[jsid].tgroup_jsid;
   if (tgroup_jsid >= 0)
   {
    params += "&tgroup_id[]="+TGroups[tgroup_jsid].tgroup_sid;
   }
   else
   {
    params += "&tgroup_id[]="+CW2_NO_OBJECT;
   }
   
   unit_jsid = Orders[jsid].unit_jsid;
   if (unit_jsid >= 0)
   {
    params += "&unit_id[]="+Units[unit_jsid].unit_sid;
   }
   else
   {
    params += "&unit_id[]="+CW2_NO_OBJECT;
   }
   
   params += "&comment[]=''";
   //params += "&comment[]='"+Orders[jsid].comment+"'";
   
   Orders[jsid].js_status = CW2_ORDER_SENDING;
  }
 }
 
 if (!num_orders_to_send)
 {
  return;
 }

 var xmlhttp = getXmlHttp();
 
 xmlhttp.open('POST', 'cw2_json.php', true);
 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlhttp.setRequestHeader("Content-length", params.length);
 xmlhttp.setRequestHeader("Connection", "close");
 
 xmlhttp.onreadystatechange = function()
 {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
  {
   dAl(1, xmlhttp.responseText, 1);
  }
 };
 xmlhttp.send(params);
}

function applyCancelOrder(order_jsid, order_hid, action_type, order_align, object_jsid)
{
 
 if (action_type == 1)
 {
  fillCurrOrderData(order_jsid, false);
  
  Orders[order_jsid].js_status = CW2_ORDER_TO_SEND;
  
  sendOrders();
 }
 
 hideOldOrder();
}

function updateOrder(jsid, tr)
{
 td = tr.childNodes[0];
 
 txt = Order_types[Orders[jsid].order_type_jsid].order_text;
 
 order_type_jsid = Orders[jsid].order_type_jsid;
 order_sid = Order_types[order_type_jsid].order_sid;
 
 if (Order_types[order_type_jsid].tplayer_permissions > 0)
 {
  txt += " - " + TPlayers[Orders[jsid].tplayer_jsid].name;
 }
 
 td.innerHTML = txt;
}

function addUpdateOrder(jsid)
{
 if (Orders[jsid].unit_jsid >= 0)
 {
  Units[Orders[jsid].unit_jsid].order_jsid = jsid;
 }
 order_type_jsid = Orders[jsid].order_type_jsid;
 order_align = Order_types[order_type_jsid].align;
 Orders[jsid].order_align = order_align;
 if (order_align & CW2_ORDER_FORT)
 {
  Planets[Orders[jsid].destination_jsid].fort_order_jsid = jsid;
 }
 
 if (!(Orders[jsid].order_hid))
 {
  Orders[jsid].order_hid = 'order_'+jsid;
 }
 
 tr = document.getElementById(Orders[jsid].order_hid);
 if (!tr)
 {
  Orders[jsid].align_hid = "???";
  if (order_align & CW2_ORDER_UNIT)
  {
   Orders[jsid].align_hid = 'unit_' + Orders[jsid].unit_jsid;
  }
  else if (order_align & CW2_ORDER_PLANET)
  {
   Orders[jsid].align_hid = 'planet_' + Orders[jsid].destination_jsid;
  }
  else if (order_align & CW2_ORDER_FORT)
  {
   Orders[jsid].align_hid = 'fort_' + Orders[jsid].destination_jsid;
  }
  else
  {
   dAl(1, "Unknown order align? align = "+order_align+"  order type = "+Orders[jsid].order_sid);
   return;
  }
  
  align_tr = document.getElementById(Orders[jsid].align_hid);
  tr = document.createElement('tr');
  tr.id = Orders[jsid].order_hid;
  tr.style.color = TGroups[Orders[jsid].tgroup_jsid].tgroup_color;
  tr.style.fontSize = "9pt";
  tr.setAttribute('onclick', 'orderClick('+jsid+')');
  
  td = document.createElement('td');
  td.colSpan = 2;
  tr.appendChild(td);
  
  insertAfter(tr, align_tr);
 }
 
 updateOrder(jsid, tr);
}

function onOrderRecruit(order_jsid, add_remove)
{
 order_sid = Orders[jsid].order_sid;
 if (!(order_sid & CW2_ORDER_RECRUIT) || (!add_remove))
 {
  return;
 }
 if (order_sid & CW2_ORDER_FORT)
 {
  TGroups[Orders[jsid].tgroup_jsid].buildings_forts += CW2_FORT_COST * add_remove;
  return;
 }
 if (order_sid & CW2_ORDER_ARMY)
 {
  TPlayers[Orders[jsid].tplayer_jsid].recruiting_armies += add_remove;
 }
 else if (order_sid & CW2_ORDER_GARRISON)
 {
  TPlayers[Orders[jsid].tplayer_jsid].recruiting_garrisons += add_remove;
 }
}

function updateRecruitCosts(p_tgroup_jsid)
{
 if (p_tgroup_jsid >= 0)
 {
  TGroups[p_tgroup_jsid].spent_credits = TGroups[p_tgroup_jsid].building_forts * CW2_FORT_COST;
 }
 else
 {
  for (tgroup_jsid in TGroups)
  {
   TGroups[tgroup_jsid].spent_credits = TGroups[tgroup_jsid].building_forts * CW2_FORT_COST;
  }
 }
 
 glicko_min = Tournament.glicko_min;
 glicko_max = Tournament.glicko_max;
 
 for (tplayer_jsid in TPlayers)
 {
  if ((p_tgroup_jsid >=0) && (TPlayers[tplayer_jsid].tgroup_jsid != p_tgroup_jsid)) {continue;}
  
  if ((TPlayers[tplayer_jsid].recruiting_armies) || (TPlayers[tplayer_jsid].recruiting_garrisons))
  {
   glicko = TPlayers[tplayer_jsid].glicko;
   num_units = TPlayers[tplayer_jsid].num_armies + TPlayers[tplayer_jsid].num_garrisons;
   base_unit_cost = CW2_MIN_UNIT_COST + ((glicko - glicko_min) / (glicko_max - glicko_min)) * CW2_GLICKO_UNIT_COST;
   recruiting_costs = 0;
   
   for (i = 0; i < TPlayers[tplayer_jsid].recruiting_armies; i++)
   {
    recruiting_costs += 2 * (base_unit_cost + num_units * CW2_COUNT_UNIT_COST);
    num_units++;
   }
   for (i = 0; i < TPlayers[tplayer_jsid].recruiting_garrisons; i++)
   {
    recruiting_costs += base_unit_cost + num_units * CW2_COUNT_UNIT_COST;
    num_units++;
   }
   
   TGroups[TPlayers[tplayer_jsid].tgroup_jsid].spent_credits += recruiting_costs;
  }
 }
}

function doOnLoad()
{
 Tournament.glicko_min = 99999;
 Tournament.glicko_max = -99999;
 for (jsid in TPlayers)
 {
  if (Tournament.glicko_max < TPlayers[jsid].glicko)
  {
   Tournament.glicko_max = TPlayers[jsid].glicko;
  }
  if (Tournament.glicko_min > TPlayers[jsid].glicko)
  {
   Tournament.glicko_min = TPlayers[jsid].glicko;
  }
 }
 dAl(0, "Tournament.glicko_min="+Tournament.glicko_min+"  Tournament.glicko_max="+Tournament.glicko_max, 1);
 
 for (jsid in Units)
 {
  if (Units[jsid].unit_flags & CW2_UNIT_GARRISON)
  {
   TPlayers[Units[jsid].tplayer_jsid].num_garrisons++;
  }
  else
  {
   TPlayers[Units[jsid].tplayer_jsid].num_armies++;
  }
 }

 for (jsid in Orders)
 {
  addUpdateOrder(jsid);
  onOrderRecruit(jsid, 1);
 }
 updateRecruitCosts(-1);
 
}

















